<?php
class DefaultIndexAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'admin';
	}

	public function execute()
	{
		//检查是否设置了淘宝客信息
		$settingService		= new SettingService();
		$this->data['app']	= $settingService->get('api_setting');
		if(empty($this->data['app']['taobao_api_appkey']) || $this->data['app']['taobao_api_appkey']=='输入你在淘宝开放平台申请的APP KEY')
		{
			//跳转到淘宝客设置页面
			header('Location:'.C('LtUrl')->generate('Site','Index'));
			return;
		}
		
		$this->data['site']		= $settingService->get('site_setting');
		$this->data['return']	= $settingService->get('return_setting');
		$this->data['list']	= $settingService->get('list_setting');
		$this->data['passwordreset'] = C('LtConfig')->get('password.reset');
		
		//检测是否有采集必须要用到的方法
		$this->data['collectenable'] = 1;
		if(!function_exists('fsockopen'))
		{
			$this->data['collectenable'] = 0;
		}
		
		$config					= C('LtConfig');
		$this->data['cache']	= $config->get('cache.product');
		$this->data['cache']['enable']	= $this->data['cache']['listcacheenable'] || $this->data['cache']['detailcacheenable'];
		
		//检查api
		$this->data['api']		= C('ApiService')->check();
		
		$du						= C('DateUtil');
		$this->data['yestoday']	= date('Y-m-d',$du->dateAdd('d','-1',time()));
		$reportService		= new ReportService();
		$returnService		= new ReturnService();
		$this->data['report']['total']		= $reportService->getTotalMoneyByPeriod();
		$this->data['return']['total']		= $returnService->getTotalMoneyByPeriod();
		$this->data['report']['yestoday']	= $reportService->getTotalMoneyByPeriod($this->data['yestoday'],$this->data['yestoday']);
		//本月
		list($startDate,$endDate)			= $du->getMonthPeriodByDate(date('Y-m-d'));
		$this->data['report']['this_month']	= $reportService->getTotalMoneyByPeriod($startDate,$endDate);
		$this->data['return']['this_month']	= $returnService->getTotalMoneyByPeriod($startDate,$endDate);
		//上月
		list($startDate,$endDate)			= $du->getMonthPeriodByDate(date('Y-m-d',$du->dateAdd('m','-1',time())));
		$this->data['report']['last_month']	= $reportService->getTotalMoneyByPeriod($startDate,$endDate);
		$this->data['return']['last_month']	= $returnService->getTotalMoneyByPeriod($startDate,$endDate);
		
		//动态
		$opts = array(
		    'http'=>array(
		    'method'=>"GET",
		    'timeout'=>3,
		  )
		);
		$context 			= stream_context_create($opts);
		$news				= file_get_contents($this->data['pisces']['url']['news'],false,$context);
		$news				= json_decode($news,true);
		$this->data['news']	= $news;
		
		
		
		$this->data['menu']		= 'default';
		$this->data['submenu']	= 'default_index';
	}
}
